package com.huateng.springcloud.utils;

import lombok.*;

/**
 * Huateng Json Result
 *
 * @author Evan
 * @create 2021/6/13 15:05
 *
 * @Description: 自定义响应数据结构
 * 				本类可提供给 H5/ios/安卓/公众号/小程序 使用
 * 				前端接受此类数据（json object)后，可自行根据业务去实现相关功能
 *
 * 				200：表示成功
 * 				500：表示错误，错误信息在msg字段中
 * 				501：bean验证错误，不管多少个错误都以map形式返回
 * 				502：拦截器拦截到用户token出错
 * 				555：异常抛出信息
 * 				556: 用户qq校验异常
 */
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class HTJsonResult {

    // 响应业务状态
    private Integer status;

    // 响应消息
    private String msg;

    // 响应中的数据
    private Object data;

//    @JsonIgnore
    private String ok;	// 不使用

    public static HTJsonResult build(Integer status, String msg, Object data) {
        return new HTJsonResult(status, msg, data);
    }

    public static HTJsonResult build(Integer status, String msg, Object data, String ok) {
        return new HTJsonResult(status, msg, data, ok);
    }

    public static HTJsonResult ok(Object data) {
        return new HTJsonResult(data);
    }

    public static HTJsonResult ok() {
        return new HTJsonResult(null);
    }

    public static HTJsonResult errorMsg(String msg) {
        return new HTJsonResult(500, msg, null);
    }

    public static HTJsonResult errorMap(Object data) {
        return new HTJsonResult(501, "error", data);
    }

    public static HTJsonResult errorTokenMsg(String msg) {
        return new HTJsonResult(502, msg, null);
    }

    public static HTJsonResult errorException(String msg) {
        return new HTJsonResult(555, msg, null);
    }

    public static HTJsonResult errorUserQQ(String msg) {
        return new HTJsonResult(556, msg, null);
    }

    public HTJsonResult(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public HTJsonResult(Object data) {
        this.status = 200;
        this.msg = "OK";
        this.data = data;
    }

    public Boolean isOK() {
        return this.status == 200;
    }
    
}
